define(['masonry', 'kindEditor'], function(require, exports, module) {
    window.editor = [];
    var config;
    function doMasonry() {
        var container = $('.u-pic');
        container.imagesLoaded(function() {
            container.masonry({
                columnWidth: 10,
                itemSelector: '.item',
                animated: true,
                height: '20px'
            });
            $('.u-pic .item').fadeIn();
        });
    }
    function bindTextArea(photoId) {
        var i = 0;
        $('.js-add-textarea').each(function () {
            var obj = $(this);
            obj.closest('form').attr('index', i);
            window.editor[i] = KindEditor.create(obj, {
                themeType: 'simple',
                uploadJson: '/Ajax/File/editorUpload',
                items: [
                    'emoticons'
                ]
            });
            i++;
        });
        $('#js-add-comment').on('submit', function() {
            var config = require('main').getConfig();
            if (!config.UserId) {
                showToLogin();
                return false;
            }
            var i = $(this).attr('index');
            editor[i].sync();
            var content = editor[i].html();
            content = content && $.trim(content);
            var text = $('<p>' + content + '</p>').text();
            var length = text.length;
            if (!length) {
                alert('请填写评论！');
                return false;
            }
            if (length < 4) {
                alert('评论至少在4个字以上！');
                return false;
            }
            if (length > 1000) {
                alert('评论太长了，不能超过1000字！');
                return false;
            }
            $.ajax({
                url: '/Ajax/Photo/addComment',
                type: 'post',
                data: {photoId: photoId, content: content},
                dataType: 'json',
                success: function(d) {
                    if (d.error) {
                        alert(d.data);
                    } else {
                        getComments(photoId, 1);
                    }
                }
            });
            return false;
        });
    }

    function bindEditPhoto() {
        $('.js-edit-btn').on('click', function() {
            var src = $('#js-detail-img').attr('src');
            var title = $('#js-detail-title').html();
            var desc = $('#js-detail-desc').html();
            require.async(['photo'], function(a) {
                a.init();
                a.showEdit(src, '', config.photoId, title, desc);
            });
        });
    }

    function getComments(photoId, page) {
        $.ajax({
            url: '/Dog/GetComments?stmp=' + new Date().toString(),
            data: {photoId: photoId, page: page},
            type: 'get',
            dataType: 'html',
            success: function(h) {
                $('#js-detail-box').html(h);
                bindTextArea(photoId);
            }
        });
    }

    exports.page = function(page) {
        getComments(config.photoId, page);
    };

    exports.init = function(c) {
        config = c;
        doMasonry();
        bindEditPhoto();
        getComments(config.photoId, 1);
        require.async('./TopInfo', function(a) {
            a.init(c);
        });
    }
});